package com.googlecode.jawb.tests.customtypes;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import com.googlecode.jawb.clues.types.DynamicLengthType;

public class CustomString implements DynamicLengthType {
	@Override
	public Object fromByteArray(InputStream inputStream) {
		try {
			String result = "";
			byte value;
			
			while ((value = (byte) inputStream.read()) != 0) {
				result += (char) value;
			}
			
			return result;
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public byte[] toByteArray(Object object) {
		return Arrays.copyOf(((String) object).getBytes(), ((String) object).length() + 1);
	}
}
